package cn.edu.sgu.www.mhxysy.service.impl;

import cn.edu.sgu.www.mhxysy.base.Pager;
import cn.edu.sgu.www.mhxysy.consts.Directories;
import cn.edu.sgu.www.mhxysy.entity.School;
import cn.edu.sgu.www.mhxysy.mapper.SchoolMapper;
import cn.edu.sgu.www.mhxysy.pager.SchoolPager;
import cn.edu.sgu.www.mhxysy.service.SchoolService;
import cn.edu.sgu.www.mhxysy.util.UploadUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author heyunlin
 * @version 1.0
 */
@Service
public class SchoolServiceImpl implements SchoolService {

	private final SchoolMapper schoolMapper;
	private final UploadUtils uploadUtils;

	@Autowired
	public SchoolServiceImpl(
			SchoolMapper schoolMapper,
			UploadUtils uploadUtils) {
		this.schoolMapper = schoolMapper;
		this.uploadUtils = uploadUtils;
	}

	@Override
	public void insert(School school) {
		school.setId(null);

		schoolMapper.insert(school);
	}

	@Override
	public void updateById(School school) {
		schoolMapper.updateById(school);
	}

	@Override
	public List<School> selectAll() {
		return schoolMapper.selectList(null);
	}

	@Override
	public School selectById(Integer id) {
		return schoolMapper.selectById(id);
	}

	@Override
	public Page<School> selectByPage(SchoolPager pager) {
		QueryWrapper<School> wrapper = new QueryWrapper<>();
		Page<School> page = Pager.ofPage(pager);

		wrapper.eq(
				pager.getPhyle() != null,
				"phyle", pager.getPhyle()
		);

		return schoolMapper.selectPage(page, wrapper);
	}

	@Override
	public String upload(MultipartFile file) throws IOException {
		String directory = Directories.DIRECTORY_SCHOOL;

		return uploadUtils.upload(file, directory);
	}

}